package com.xwj.exception.solution_3.demo.controller;

import com.alibaba.fastjson.JSON;
import com.xwj.common.util.R;
import com.xwj.exception.solution_3.demo.dto.CustomList;
import com.xwj.exception.solution_3.demo.dto.StudentDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.URL;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;

/**
 * @Author: xiaowajiang
 * @Date: 2022-04-05 15:51
 * @Description:
 **/
@Slf4j
@RestController
@Validated
@Api(value = "ParamCheckController", tags = "方案3 - 统一异常处理(参数校验)")
public class ParamCheckController {

    @ApiOperation(value = "请求体发送 JSON 数据", position = 1)
    @PostMapping("solution_3/param/valid/student/save")
    public R save(@Valid @RequestBody StudentDTO stu) {
        log.info("保存学员信息，入参：{}", JSON.toJSONString(stu));
        // 业务逻辑
        return R.ok();
    }

    @ApiOperation(value = "请求体发送 form-data 数据", position = 2)
    @PostMapping("solution_3/param/valid/student/update")
    public R update(@Valid StudentDTO stu) {
        log.info("修改学员信息，入参：{}", JSON.toJSONString(stu));
        // 业务逻辑
        return R.ok();
    }

    @ApiOperation(value = "URL Query String传参", position = 3)
    @PostMapping("solution_3/param/valid/student/update/photo")
    public R updatePhoto(@RequestParam Long id, @URL @RequestParam String photo) {
        // 业务逻辑
        return R.ok();
    }

    @ApiOperation(value = "Path Info传参", position = 4)
    @PostMapping("solution_3/param/valid/student/info/{id}")
    public R updatePhoto(@Min(10000) @PathVariable Long id) {
        log.info("查询学员信息，入参：{}", id);
        // 业务逻辑
        return R.ok();
    }

    @ApiOperation(value = "集合校验", position = 5)
    @PostMapping("solution_3/param/valid/saveList")
    public R saveList(@RequestBody @Validated CustomList<StudentDTO> list) {
        log.info("保存学员信息，入参：{}", JSON.toJSONString(list));
        // 业务逻辑处理
        return R.ok();
    }

}
